//
//  CZFoodsViewController.m
//  UISplitViewController练习
//
//  Created by chao on 15/2/28.
//  Copyright (c) 2015年 chao. All rights reserved.
//

#import "CZFoodsViewController.h"
#import "CZFood.h"
#import "CZFoodTyoeViewController.h"
#import "CZFoodType.h"
#import "MJExtension.h"
#import "CZFoodDetailViewController.h"

@interface CZFoodsViewController ()<CZFoodTyoeViewControllerDelegate, UISplitViewControllerDelegate>
@property (strong, nonatomic) NSArray *foods;

@end

@implementation CZFoodsViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.foods.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"food";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    CZFood *foods = self.foods[indexPath.row];
    cell.textLabel.text = foods.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"难度: %@  时间: %@", foods.diff, foods.time];
    return cell;
}
- (void)foodTyoeViewController:(CZFoodTyoeViewController *)foodTypeVc didSelectedFoodType:(CZFoodType *)type {

    self.foods = [CZFood objectArrayWithFilename:[NSString stringWithFormat:@"type_%@_foods.plist", type.idstr]];
    self.title = type.name;
    [self.tableView reloadData];
}

//- (void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode {
//
//
//}


- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc {

    self.navigationItem.leftBarButtonItem = barButtonItem;
}

- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
    self.navigationItem.leftBarButtonItem = nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CZFood *food = self.foods[indexPath.row];
    CZFoodDetailViewController *detail = [[CZFoodDetailViewController alloc] init];
    detail.food = food;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:detail];
    nav.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:nav animated:YES completion:nil];
}
@end
